package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import po.*;
import service.AdminService;
import utils.ResUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;

// restful 风格的请求
//  url 不能有动词，只有复数名词

//    GET:      http://www.example.com/users		获取用户列表数据
//    POST:	    http://www.example.com/users		创建(添加)用户数据
//    GET:	    http://www.example.com/users/1		获取用户ID为1的用户信息
//    PUT:	    http://www.example.com/users/1		修改用户ID为1的用户信息
//    DELETE:	http://www.example.com/users/1		删除用户ID为1的用户信息


    // 获取管理员列表数据
    @GetMapping("/admins")
    @ResponseBody
    public Object AllAdmin(HttpServletRequest req) {
        String username = req.getParameter("username").trim();
        String sex = req.getParameter("sex").trim();
        String roleId = req.getParameter("roleId").trim();
        String status = req.getParameter("status").trim();

        Admin admin = new Admin();

        if (username != "") {
            admin.setUsername(username);
        }
        if (sex != "") {
            admin.setSex(new Byte(sex));
        }
        if (roleId != "") {
            Role role = new Role();
            role.setId(Integer.parseInt(roleId));
            admin.setRole(role);
        }
        if (status != "") {
            admin.setIsEnable(new Byte(status));
        }

         List<Admin> res = this.adminService.allAdmin(admin);

        if (res != null) {
            return ResUtils.Success(1,"查询成功",res);
        } else {
            return ResUtils.Fail(0,"没有数据");
        }
    }

    // 根据 id 获取管理员信息 登录的
    @GetMapping("/admins/{id}")
    @ResponseBody
    public Object AdminOfId(@PathVariable("id") Integer id) {

        Admin res = adminService.adminOfId(id);
        System.out.println(res);
        if (res != null) {
            return ResUtils.Success(1,"查询成功",res);
        } else {
            return ResUtils.Fail(0,"没有数据");
        }
    }
    // 根据 id 获取管理员信息 修改的
    @GetMapping("/admins/id/{id}")
    @ResponseBody
    public Object getAdminOfId(@PathVariable("id") Integer id) {

        Admin res = adminService.getAdminOfId(id);
        System.out.println(res);
        if (res != null) {
            return ResUtils.Success(1,"查询成功",res);
        } else {
            return ResUtils.Fail(0,"没有数据");
        }
    }

    // 删除用户
    @DeleteMapping("/admins/{id}")
    @ResponseBody
    public Object deleteAdmin(@PathVariable("id") Integer id) {
        boolean res = adminService.deleteAdmin(id);
        if (res) {
            return ResUtils.Success(1,"删除成功");
        } else {
            return ResUtils.Fail(0,"删除失败");
        }
    }

    // 根据 id 修改状态管理员信息
    @PutMapping("/admins/status/{id}")
    @ResponseBody
    public Object isEnableAdmin(@PathVariable("id") Integer id,@RequestParam("isEnable") Integer isEnable) {
//        System.out.println(id+"接收到的id " +isEnable);
        Integer flag = isEnable == 1 ? 0 : 1;
        Boolean res = adminService.isEnableAdmin(id,flag);
        if (res) {
            return ResUtils.Success(1, "状态成功");
        } else {
            return ResUtils.Fail(0, "状态失败");
        }
    }

    //  添加用户
    @PostMapping("/admins")
    @ResponseBody
    public Object addAdmin(@RequestBody Admin admin){

        Date now = new Date();
        admin.setCreateTime(now);
        admin.setUpdateTime(now);
        System.out.println(admin);
        boolean res = adminService.addAdmin(admin);

        if (res) {
            return ResUtils.Success(1,"添加成功",admin);
        } else {
            return ResUtils.Fail(0,"注册失败");
        }
    }


    // 根据 id 修改用户信息
    @PutMapping("/admins/{id}")
    @ResponseBody
    public Object editAdmin(@PathVariable("id") Integer id,@RequestBody Admin admin) {

        Date now = new Date();
        admin.setUpdateTime(now);
        System.out.println(admin);

        Boolean res = adminService.editAdmin(admin);

        if (res) {
            return ResUtils.Success(1, "修改成功",admin);
        } else {
            return ResUtils.Fail(0, "修改失败");
        }
    }

//  判断是否登录成功
    @PostMapping("/admin/login")
    @ResponseBody
    public Object login(HttpServletRequest req) {
        String username = req.getParameter("username").trim();
        String password = req.getParameter("password").trim();
        boolean res = adminService.veritypasswd(username, password);
        if (res) {
            Admin data = adminService.loginStatus(username);
            return ResUtils.Success(200, "登录成功",data);
        } else {
            return ResUtils.Fail(0, "用户名或密码错误或账号被禁用");
        }
    }


}
